package com.longcai.controller.goods;


import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longcai.bean.PageHelper;
import com.longcai.entity.homegood.ClyjGoodsBrand;
import com.longcai.entity.homegood.ClyjGoodsBrandType;
import com.longcai.entity.homepage.GoodsClass;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.goods.ClyjGoodsBrandService;
import com.longcai.util.OSSUtils;

import io.swagger.annotations.ApiOperation;

import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 商品品牌表 前端控制器
 * </p>
 *
 * @author LWK
 * @since 2019-03-29
 */
@Controller
@RequestMapping("/clyjGoodsBrand/")
public class ClyjGoodsBrandController {
	private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjGoodsBrandService bannerService;

    /**
     * 分页查询品牌
     *
     * @return
     */
    @GetMapping(value="getBrand")
    @ResponseBody
    public PageTableResponse saveGoodSolr(ClyjGoodsBrand sysRole, PageHelper pageHelper) {
    	Page<ClyjGoodsBrand> page = new Page<ClyjGoodsBrand>(pageHelper.getStart(), pageHelper.getLength());
    	QueryWrapper<ClyjGoodsBrand> w = new QueryWrapper<ClyjGoodsBrand>();
    	if(sysRole.getBrandName()!=null && sysRole.getBrandName()!="")
    		w.like("brand_name", sysRole.getBrandName());
    	w.orderByAsc("brand_sort");
        IPage<ClyjGoodsBrand> sysRoleIPage = this.bannerService.selectPage(page, w);
        PageTableResponse pageTableResponse = new PageTableResponse(sysRoleIPage.getTotal(),
                sysRoleIPage.getTotal(), sysRoleIPage.getRecords());
         return pageTableResponse;
    }
    
    /**
     * 分页查询品牌
     *
     * @return
     */
    @GetMapping(value="getBrand2")
    @ResponseBody
    public List<ClyjGoodsBrand> getBrand2(ClyjGoodsBrand sysRole, PageHelper pageHelper) {
    	QueryWrapper<ClyjGoodsBrand> w = new QueryWrapper<ClyjGoodsBrand>();
    	if(sysRole.getBrandName()!=null && sysRole.getBrandName()!="")
    		w.like("brand_name", sysRole.getBrandName());
    	w.orderByAsc("brand_sort");
        List<ClyjGoodsBrand> sysRoleIPage = this.bannerService.selectList(w);
         return sysRoleIPage;
    }
    
    @PostMapping(value = "saveClass")
    @ResponseBody
    public boolean save( ClyjGoodsBrandType goodsClas2s,MultipartFile banner) {
    	if(banner!=null){
    		//文档上传oss
        	try {
        		String fileName = banner.getOriginalFilename();
        		String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
    			InputStream inputStream = banner.getInputStream();
    			Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/GOODSBRAND/");
    			goodsClas2s.setBrandIcon(uploadByInputStream.get("path").toString());
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	if(goodsClas2s.getBrandId()==null || goodsClas2s.getBrandId()==""){
    		//新增
    		goodsClas2s.setBrandId(UUID.randomUUID().toString());
    		goodsClas2s.setCreateTime(new Date());
    		this.bannerService.insert(goodsClas2s);
    		
    		
    	}else{
    		this.bannerService.updateById(goodsClas2s);
    	}
    	//删除分类信息
    	bannerService.deleteType(goodsClas2s);
    	//新增分类信息
    	bannerService.insertType(goodsClas2s);
        return true;
    }
    
	
    @GetMapping("{id}")
    @ApiOperation(value = "根据id获取分类")
    @ResponseBody
    public ClyjGoodsBrand get(@PathVariable String id) {
        return this.bannerService.selectById(id);
    }

    
    @DeleteMapping("{id}")
    @ApiOperation(value = "删除品牌")
    @ResponseBody
    @PreAuthorize("hasAuthority('goods:type:del')")
    public void delete(@PathVariable String id) {
        this.bannerService.deleteById(id);
    }
    
    @GetMapping(value = "brandId")
    @ResponseBody
    @ApiOperation(value = "根据brandid获取分类")
    public List<GoodsClass> listByBrandId(String brandId) {
        return this.bannerService.listByBrandId(brandId);
    }
}

